package me.jessyan.mvparms.demo.mvp.ui.adapter;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothDevice;
import android.net.wifi.ScanResult;
import android.view.View;
import android.widget.TextView;

import com.jess.arms.base.BaseHolder;
import com.jess.arms.base.DefaultAdapter;
import com.platform.ai.control.R;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import me.jessyan.mvparms.demo.mvp.model.entity.BluRxBean;
import me.jessyan.mvparms.demo.mvp.model.logic.TextUtil;

/**
 * Created by 95 on 2017/9/5.
 */
public class ScanBlueToothAdapter extends DefaultAdapter<BluetoothDevice> {
    ConfigBlueToothListener mListener;
    public ScanBlueToothAdapter(List<BluetoothDevice> infos, ConfigBlueToothListener mListener) {
        super(infos);
        this.mListener = mListener;
    }


    @Override
    public BaseHolder getHolder(View v, int viewType) {
        return new DeviceHolder(v,mListener);
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_bluetooth_devices;
    }

    public class DeviceHolder extends BaseHolder<BluetoothDevice> {

        @BindView(R.id.devicename)
        TextView mBlueToothName;

        @BindView(R.id.statue)
        TextView mBlueToothState;

        private View mView;

        ConfigBlueToothListener mListener;
        public DeviceHolder(View itemView,ConfigBlueToothListener mListener) {
            super(itemView);
            this.mListener = mListener;
        }


        @SuppressLint("StringFormatInvalid")
        @Override
        public void setData(BluetoothDevice bean, int position) {
            mView = itemView;
            if(TextUtil.isEmpty(bean.getName())){
                mBlueToothName.setText(bean.getAddress());
            }else {
                mBlueToothName.setText(bean.getName());
            }
            if (bean.getBondState() == BluetoothDevice.BOND_BONDED) {
                mBlueToothState.setText("已配对");
            } else if(bean.getBondState() == BluetoothDevice.BOND_BONDING){
                mBlueToothState.setText("配对中...");
            } else{
                mBlueToothState.setText("未配对");
            }
            mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.configBlueTooth(bean,position);
                }
            });
        }
    }

    public interface ConfigBlueToothListener{
        void configBlueTooth(BluetoothDevice bean,int position);
    }

}
